package com.cipe.cmrs.converter;

import org.apache.commons.lang.BooleanUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;

import com.cipe.cmrs.bean.AuditsBean;
import com.cipe.cmrs.bean.ImpBean;
import com.cipe.cmrs.model.Audits;
import com.cipe.cmrs.model.Imp;

public class AuditsToAuditsBean implements Converter<Audits, AuditsBean> {

    private ConverterRegistry converterRegistry;

    public void setConverterRegistry(ConverterRegistry converterRegistry) {
        this.converterRegistry = converterRegistry;
    }

    public void init() {
        converterRegistry.addConverter(this);
    }

    public AuditsBean convert(Audits source) {
        AuditsBean bean = new AuditsBean();
        if (source != null) {
            bean.setId(source.getId());
            bean.setNewgrantee(BooleanUtils.toBoolean(source.getNewgrantee()));
            if (source.getRiskassessment() != null) {
                bean.setRiskassessment(source.getRiskassessment());
            }
            bean.setSmponly(BooleanUtils.toBoolean(source.getSmponly()));
            bean.setInternalmonitoring(BooleanUtils.toBoolean(source.getInternalmonitoring()));
            bean.setInterimaudit(BooleanUtils.toBoolean(source.getInterimaudit()));
            bean.setFinalaudit(BooleanUtils.toBoolean(source.getFinalaudit()));

            // Internal Monitoring
            bean.setImprequired(BooleanUtils.toBoolean(source.getImprequired()));
            bean.setImpnotes(source.getImpnotes());
            if (source.getImps() != null) {
                for (Imp imp : source.getImps()) {
                    bean.getImpBeans().add(toImpBean(imp));
                }
            }

            // Interim Audit
            bean.setIasolicitor(source.getIasolicitor());

            bean.setIaselectionmemodue(source.getIaselectionmemodue());
            bean.setIaselectionmemoreceived(source.getIaselectionmemoreceived());
            bean.setIaselectionmemoapproved(source.getIaselectionmemoapproved());

            bean.setIafirm(source.getIafirm());

            bean.setIadue(source.getIadue());
            bean.setIareceived(source.getIareceived());
            bean.setIaapproved(source.getIaapproved());

            bean.setIarevisedresolutiondue(source.getIarevisedresolutiondue());
            bean.setIarevisedresolutionreceived(source.getIarevisedresolutionreceived());
            bean.setIarevisedresolutionapproved(source.getIarevisedresolutionapproved());

            bean.setIarevisedresolutionoffindingsdue(source.getIarevisedresolutionoffindingsdue());
            bean.setIarevisedresolutionoffindingsreceived(source.getIarevisedresolutionoffindingsreceived());
            bean.setIarevisedresolutionoffindingsapproved(source.getIarevisedresolutionoffindingsapproved());

            bean.setIaresolutionoffindingsdue(source.getIaresolutionoffindingsdue());
            bean.setIaresolutionoffindingsreceived(source.getIaresolutionoffindingsreceived());
            bean.setIaresolutionoffindingsapproved(source.getIaresolutionoffindingsapproved());

            bean.setIamemonotes(source.getIamemonotes());
            bean.setIaclosedtofile(source.getIaclosedtofile());

            // Final Audit
            bean.setFasolicitor(source.getFasolicitor());
            bean.setFaselectionmemoduedate(source.getFaselectionmemoduedate());
            bean.setFaselectionmemodue(source.getFaselectionmemodue());
            bean.setFaselectionmemoreceived(source.getFaselectionmemoreceived());
            bean.setFaselectionmemoapproved(source.getFaselectionmemoapproved());

            bean.setFafirm(source.getFafirm());

            bean.setFadue(source.getFadue());
            bean.setFareceived(source.getFareceived());
            bean.setFaapproved(source.getFaapproved());

            bean.setFarevisedresolutiondue(source.getFarevisedresolutiondue());
            bean.setFarevisedresolutionreceived(source.getFarevisedresolutionreceived());
            bean.setFarevisedresolutionapproved(source.getFarevisedresolutionapproved());

            bean.setFarevisedresolutionoffindingsdue(source.getFarevisedresolutionoffindingsdue());
            bean.setFarevisedresolutionoffindingsreceived(source.getFarevisedresolutionoffindingsreceived());
            bean.setFarevisedresolutionoffindingsapproved(source.getFarevisedresolutionoffindingsapproved());

            bean.setFaresolutionoffindingsdue(source.getFaresolutionoffindingsdue());
            bean.setFaresolutionoffindingsreceived(source.getFaresolutionoffindingsreceived());
            bean.setFaresolutionoffindingsapproved(source.getFaresolutionoffindingsapproved());

            bean.setFamemonotes(source.getFamemonotes());
            bean.setFaclosedtofile(source.getFaclosedtofile());
        }
        return bean;
    }

    private ImpBean toImpBean(Imp imp) {
        ImpBean bean = new ImpBean();
        bean.setId(imp.getId());
        bean.setAction(imp.getImpaction());
        bean.setClosedtofile(imp.getImpclosedtofile());
        bean.setDuedate(imp.getImpduedate());
        bean.setPerformed(imp.getImpperformed());
        bean.setReceived(imp.getImpreceived());
        return bean;
    }
}
